package com.wsj.web.action.xml;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wsj.util.tool.Dom4jDemo;
import com.wsj.util.tool.FileUplaodAndManageForStorage;
import com.wsj.vo.homeSiteXml.ItemVo;
import com.wsj.web.form.xml.ItemVoForm;

public class ItemVoAction extends DispatchAction {
	
	
	private FileUplaodAndManageForStorage imgUpload;
	
	private Dom4jDemo dom4jDemo;
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		List list=new ArrayList();
		String path=request.getSession().getServletContext().getRealPath("/");
		list=Dom4jDemo.loadItemList(path+"fileUpload/flashPic/js/homeSite_pic.xml");
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			request.setAttribute("list", list);
		}
		return mapping.findForward("list");
		
	}
	public ActionForward toupdate(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ItemVoForm itemForm=(ItemVoForm)form;
		String id=request.getParameter("id");
		String item_url=request.getParameter("item_url");
		String link=request.getParameter("link");
		String titleName=request.getParameter("titleName");
		
		
		if(id!=null&&!id.equals("")){
			
			request.setAttribute("id", id);
			itemForm.setId(id);
		}
		if(item_url!=null&&!item_url.equals("")){
			request.setAttribute("item_url", item_url);
			itemForm.setItem_url(item_url);
			
		}
		if(link!=null&&!link.equals("")){
			
			request.setAttribute("link", link);
			itemForm.setLink(link);
		}
		
		if(titleName!=null  && !titleName.equals(""))
		{
			request.setAttribute("titleName",new String(titleName.getBytes("ISO-8859-1"),"UTF-8"));
			itemForm.setTitleName(new String(titleName.getBytes("ISO-8859-1"),"UTF-8"));
//			
//			String aa =new String(titleName.getBytes("ISO-8859-1"),"UTF-8");
		}
		request.setAttribute("itemForm", itemForm);
		return mapping.findForward("toupdate");
	}
	
	
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception 
	{
		
		//上传图片
		ItemVoForm itemForm=(ItemVoForm)form;
		ItemVo itemvo=new ItemVo();
		if(itemForm.getPic()!=null&&!itemForm.getPic().getFileName().trim().equals(""))
		{
			String uploadPath="fileUpload"+File.separator+"flashPic"+File.separator;
			imgUpload.setMyFiles(itemForm.getPic());
			imgUpload.setPath(uploadPath);
			String fileName=imgUpload.uploadFile(request, imgUpload.loadUploadFileName());
			itemvo.setItem_url("/fileUpload/flashPic/"+fileName);
		}else
		{
			itemvo.setItem_url(request.getParameter("item_url"));
		}
		String id=request.getParameter("id");
		String link=request.getParameter("link");
		String titleName=request.getParameter("titleName");
		itemvo.setId(id);
		itemvo.setLink(link);
		itemvo.setTitleName(titleName);
		String path=request.getSession().getServletContext().getRealPath("/");
		String fileName=path+"fileUpload/flashPic/js/homeSite_pic.xml";
		String newfilename=path+"fileUpload/flashPic/js/homeSite_pic.xml";
//		int b=Dom4jDemo.ModiXMLFile(fileName, newfilename,itemvo);
		
		String[] attributeName =new String[]{"item_url","link","titleName"};
		String[] attributeValue =new String[]{itemvo.getItem_url(),itemvo.getLink(),itemvo.getTitleName()};
//		book[@type='society'] @后面是属性名=值
		String xPath="//item[@id="+id+"]";
		int b=dom4jDemo.updateXMLFileAttribute(fileName, newfilename, xPath, attributeName,attributeValue);
		if(b==1){
			request.setAttribute("update", "<script>alert('修改成功！！！！');</script>");
		}
		
		return new ActionForward("/dom4jDemo.do?method=list");
		
	}
	
	
	
	public ActionForward toadd(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
		String path=request.getSession().getServletContext().getRealPath("/");
		List list=new ArrayList();
		list=Dom4jDemo.loadItemList(path+"fileUpload/flashPic/js/homeSite_pic.xml");
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			request.setAttribute("id", list.size()+1);
			Comparator com=new Comparator()
			{

				public int compare(Object arg0, Object arg1) {
					// TODO Auto-generated method stub
					ItemVo itemvo1=(ItemVo)arg0;
					ItemVo itemvo2=(ItemVo)arg1; 
					if(Integer.valueOf(itemvo1.getId())>Integer.valueOf(itemvo2.getId())){
						return 1;
					}
					else
					{
						return 0;
					}
				}
				
			};
			
			Collections.sort(list,com);
			Object max = Collections.max(list,com);
			ItemVo itemvo3=(ItemVo) max;
			request.setAttribute("id", Integer.valueOf(itemvo3.getId())+1);
			
			
		}
		else 
		{
			request.setAttribute("id", 1);
		}
		return mapping.findForward("toadd");
		
	}
	
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ItemVoForm itemForm=(ItemVoForm)form;
		boolean isok=this.checkForm(request,itemForm);
		if(!isok){
			request.setAttribute("add", "<script>alert('添加失败，请认真填写各项！！！！');</script>");
			return new ActionForward("/dom4jDemo.do?method=toadd");
		}
		//上传图片
		ItemVo itemvo=new ItemVo();
		if(itemForm.getPic()!=null&&!itemForm.getPic().getFileName().trim().equals(""))
		{
			String uploadPath="fileUpload"+File.separator+"flashPic"+File.separator;
			imgUpload.setMyFiles(itemForm.getPic());
			imgUpload.setPath(uploadPath);
			String fileName=imgUpload.uploadFile(request, imgUpload.loadUploadFileName());
			itemvo.setItem_url("/fileUpload/flashPic/"+fileName);
		}
		String id=request.getParameter("id");
		String link=request.getParameter("link");
		String titleName=request.getParameter("titleName");
		itemvo.setId(id);
		itemvo.setLink(link);
		itemvo.setTitleName(titleName);
		String path=request.getSession().getServletContext().getRealPath("/");
		String fileName=path+"fileUpload/flashPic/js/homeSite_pic.xml";
		String newfilename=path+"fileUpload/flashPic/js/homeSite_pic.xml";
		int b=Dom4jDemo.addXMLFile(fileName, newfilename,itemvo);
		if(b==1){
			request.setAttribute("add", "<script>alert('添加成功！！！！');</script>");
		}
		
		return new ActionForward("/dom4jDemo.do?method=toadd");
		
	}
	
	private boolean checkForm(HttpServletRequest request, ItemVoForm itemForm) {
		// TODO Auto-generated method stub
		boolean isok=true;
		String id=request.getParameter("id");
		String link=request.getParameter("link");
		if(id==null||id.equals("")){
			isok=false;
		}else if(isok&&(link==null||link.equals(""))){
			isok=false;
		}else if(isok&&(itemForm.getPic().getFileName()==null||itemForm.getPic().getFileName().equals(""))){
			isok=false;
		}
		return isok;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ItemVo itemvo=new ItemVo();
		String id=request.getParameter("id");
		itemvo.setId(id);
		
		String path=request.getSession().getServletContext().getRealPath("/");
		
		String fileName=path+"fileUpload/flashPic/js/homeSite_pic.xml";
		String newfilename=path+"fileUpload/flashPic/js/homeSite_pic.xml";
		int b=Dom4jDemo.deleteXMLFile(fileName, newfilename,itemvo);
		if(b==1){
			request.setAttribute("succe", "<script>alert('删除成功！！！！');</script>");
		}
		
		return new ActionForward("/dom4jDemo.do?method=list");
		
	}
	
	
	public Dom4jDemo getDom4jDemo() {
		return dom4jDemo;
	}

	public void setDom4jDemo(Dom4jDemo dom4jDemo) {
		this.dom4jDemo = dom4jDemo;
	}

	public FileUplaodAndManageForStorage getImgUpload() {
		return imgUpload;
	}

	public void setImgUpload(FileUplaodAndManageForStorage imgUpload) {
		this.imgUpload = imgUpload;
	}

}
